import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from datetime import datetime,timedelta
from config import datas


class Testlogging:
    def test_get_login_logging(self, get_env_info):
        # 检查日志模块登录成功
        url = datas.baseUrl + '/container_platform/logging/logSearch'
        req = RequestUtil().send_request('get', url)
        assert req.status_code == 200

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/1_get_logging_of_namespace.yaml'))
    def test_get_logging_of_namespace(self, logginginfo, get_env_info):
        # 查询命名空间下的日志
        baseUrl = get_env_info + "/clusters/host"
        starttime = str((datetime.now() - timedelta(minutes=10)).timestamp()).split('.')[0]
        endtime = str(datetime.now().timestamp()).split('.')[0]
        if '{keyword}' in logginginfo['request']['url']:
            url = baseUrl + logginginfo['request']['url'].replace('{namespace}','monitoring').replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime)).replace('{level}','error').replace('{keyword}','error')
        else:
            url = baseUrl + logginginfo['request']['url'].replace('{namespace}','monitoring').replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime))
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/2_get_logging_of_pod.yaml'))
    def test_get_logging_of_pod(self, logginginfo, get_env_info):
        # 查询pod下的日志
        baseUrl = get_env_info + "/clusters/host"
        starttime = str((datetime.now() - timedelta(minutes=10)).timestamp()).split('.')[0]
        endtime = str(datetime.now().timestamp()).split('.')[0]
        if '{keyword}' in logginginfo['request']['url']:
            url = baseUrl + logginginfo['request']['url'].replace('{namespace}','monitoring').replace('{pod}','alertmanager-main-0').replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime)).replace('{level}','error').replace('{keyword}','error')
        else:
            url = baseUrl + logginginfo['request']['url'].replace('{namespace}','monitoring').replace('{pod}','alertmanager-main-0').replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime))
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/3_get_logging_of_container.yaml'))
    def test_get_logging_of_container(self, logginginfo, get_env_info):
        # 查询container下的日志
        baseUrl = get_env_info + "/clusters/host"
        starttime = str((datetime.now() - timedelta(minutes=10)).timestamp()).split('.')[0]
        endtime = str(datetime.now().timestamp()).split('.')[0]
        if '{keyword}' in logginginfo['request']['url']:
            url = baseUrl + logginginfo['request']['url'].replace('{namespace}','monitoring').replace('{pod}','alertmanager-main-0').replace('{container}','alertmanager').replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime)).replace('{level}','error').replace('{keyword}','error')
        else:
            url = baseUrl + logginginfo['request']['url'].replace('{namespace}','monitoring').replace('{pod}','alertmanager-main-0').replace('{container}','alertmanager').replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime))
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/4_get_logging_filelist.yaml'))
    def test_get_logging_filelist(self, logginginfo, get_env_info):
        # 查询日志文件列表
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + logginginfo['request']['url']
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/5_get_logging_resourcelist.yaml'))
    def test_get_logging_resourcelist(self, logginginfo, get_env_info):
        # 查询资源列表
        baseUrl = get_env_info + "/clusters/host"
        if '{namespace}' in logginginfo['request']['url']:
            url = baseUrl + logginginfo['request']['url'].replace('{namspace}','default')
        else:
            url = baseUrl + logginginfo['request']['url']
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/6_get_logging_by_filename.yaml'))
    def test_get_logging_by_filename(self, logginginfo, get_env_info):
        # 根据文件名查询日志
        baseUrl = get_env_info + "/clusters/host"
        starttime = str((datetime.now() - timedelta(minutes=10)).timestamp()).split('.')[0]
        endtime = str(datetime.now().timestamp()).split('.')[0]
        url = baseUrl + logginginfo['request']['url'].replace('{starttime}',str(starttime)).replace('{endtime}',str(endtime)).replace('{level}','error').replace('{filename}',datas.filename)
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/7_get_logging_sources.yaml'))
    def test_get_logging_sources(self, logginginfo, get_env_info):
        # 采集任务相关接口
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + logginginfo['request']['url']
        method = logginginfo['request']['method']
        if 'headers' in logginginfo['request']:
            req = RequestUtil().send_request(method, url, headers=logginginfo['request']['heders'])
        else:
            req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/8_get_logging_context_details.yaml'))
    def test_get_logging_sources(self, logginginfo, get_env_info):
        # 获取日志上下文详情
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + logginginfo['request']['url'].replace('{filename}',datas.filename)
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('logginginfo', YamlUtil().read_testcase_yaml('v24.09/logging/9_get_logging_other.yaml'))
    def test_get_logging_sources(self, logginginfo, get_env_info):
        # 获取日志其他接口
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + logginginfo['request']['url']
        method = logginginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == logginginfo['validate'][0]['equals']['status_code']